Add tenant query filtering in TenantInterceptor via OnQuery - #95
Conversation
Co-authored-by: nygula <13066114+nygula@users.noreply.github.com>
|
须加测如下场景 : 1.有租户 + 已有whereexpression的情况 是否自动过滤 2. 有租户.纯FindAll() Find()等无whereexpression的情况 是否空查询也过滤 3. tennatid=0的情况 是否可见全部 4. 未设置租户上下文的情况 是否会报错 @copilot |
TenantInterceptor via OnQuery
Co-authored-by: nygula <13066114+nygula@users.noreply.github.com>
已补充你提到的四类场景测试并提交到
|
There was a problem hiding this comment.
Pull request overview
Adds query-time tenant filtering to the XCode interceptor pipeline so entities implementing ITenantScope automatically get a TenantId constraint injected during query construction, and extends the existing tenant test suite to cover this query behavior.
Changes:
- Added
TenantInterceptor.OnQuery(IEntityFactory, Expression?, QueryAction)that normalizes/merges predicates and appliesApplyTenant(factory). - Added unit tests validating tenant filter injection, predicate merging, and no-op behavior when tenant context is absent /
TenantId == 0.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| XCode/Membership/ITenantSource.cs | Adds TenantInterceptor.OnQuery to inject tenant filtering into query predicates. |
| XUnitTest.XCode/Membership/TenantContextTests.cs | Adds tests covering tenant query filtering behavior through the interceptor. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| protected override XCode.Expression? OnQuery(IEntityFactory factory, XCode.Expression? where, QueryAction action) | ||
| { | ||
| return (where & XCode.Expression.Empty).ApplyTenant(factory); | ||
| } | ||
| } |
| protected override XCode.Expression? OnQuery(IEntityFactory factory, XCode.Expression? where, QueryAction action) | ||
| { | ||
| return (where & XCode.Expression.Empty).ApplyTenant(factory); | ||
| } |
| [Fact] | ||
| [DisplayName("OnQuery_有租户且已有WhereExpression时合并租户条件")] | ||
| public void TenantModule_OnQuery_WithTenantAndWhereExpression_MergesTenantFilter() | ||
| { | ||
| // Arrange | ||
| var module = new TenantInterceptor(); | ||
| TenantContext.Current = new TenantContext { TenantId = 456 }; | ||
| var factory = TenantTestEntity.Meta.Factory; | ||
| var where = new WhereExpression(); | ||
| where &= TenantTestEntity._.Name == "Stone"; | ||
|
|
TenantInterceptoronly handled create/validate flows, so tenant-scoped entities were not consistently filtered during query construction. This change adds query-time tenant condition injection at the interceptor layer so tenant filtering is applied automatically for registered entities implementingITenantScope.Interceptor query-path fix
OnQuery(IEntityFactory, Expression?, QueryAction)toTenantInterceptor.where(includingnull) and appends tenant constraints through existingApplyTenant(factory)logic.ApplyTenantcalls in business code.Focused coverage for tenant query behavior
TenantContextTestsfor: